//获取应用实例
const app = getApp();

Page({
	data: {
    bindwechatAccount:'当前尚未绑定任何微信账号',
    codeId:'',//验证码
    name:'',//姓名
    accountId:'',//微信号
    codename: '获取验证码',
    wx:'',
  },
	onLoad: function() {
    this.getUserMsg();
  },

  getUserMsg() {
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/User/getMsg',
      data: {
        token: app.globalData.token,
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        if (res.data.data.user_detail.weixin != null) {
          that.setData({
            bindwechatAccount: '已绑定微信号：' + res.data.data.user_detail.weixin
          })
        } else {

        }
      }, fail: function () {

      }
    });
  },

  codeInput: function (e) {
    var that = this;
    that.setData({
      codeId: e.detail.value
    })
  },
  nameInput: function (e) {
    var that = this;
    that.setData({
      name: e.detail.value
    })
  },
  accountInput: function (e) {
    var that = this;
    that.setData({
      accountId: e.detail.value
    })
  },

//获取验证码
  getcode(){
    var _this=this;
    if (_this.data.codeId.length==0){
      wx.showToast({
        title: '请填写验证码',
        icon:'none'
      })
      return;
    }
    if (_this.data.name.length == 0) {
      wx.showToast({
        title: '请填写真实姓名',
        icon: 'none'
      })
      return;
    }
    if (_this.data.accountId.length == 0) {
      wx.showToast({
        title: '请填写账号',
        icon: 'none'
      })
      return;
    }
    
    wx.request({
      url: app.globalData.api + '/index.php/wap/Sms/sendWxCode',
      data: {
        'token': app.globalData.token,//手机号码
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        if(res.data.code==1){
          var num = 61;
          var timer = setInterval(function () {
            num--;
            if (num <= 0) {
              clearInterval(timer);
              _this.setData({
                codename: '重新发送',
                disabled: false
              })

            } else {
              _this.setData({
                codename: num + "s"
              })
            }
          }, 1000)
        }
        wx.showToast({
          title: '' + res.data.msg,//提示文字
          duration: 2000,//显示时长
          mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
          icon: 'success', //图标，支持"success"、"loading"  
          success: function () { },//接口调用成功
          fail: function () { },  //接口调用失败的回调函数  
          complete: function () { } //接口调用结束的回调函数  
        })
        
      }
    })
  },


//绑定微信
  bindWechat:function(){
    var that=this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/User/bandingWx',
      data: {
        token: app.globalData.token,//手机号码
        wx:that.data.accountId,
        code:that.data.codeId,
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        if(res.data.code==1){
          wx.navigateBack({
            delta:1
          })
        }
        wx.showToast({
          title: '' + res.data.msg,//提示文字
          duration: 2000,//显示时长
          mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
          icon: 'success', //图标，支持"success"、"loading"  
          success: function () { },//接口调用成功
          fail: function () { },  //接口调用失败的回调函数  
          complete: function () { } //接口调用结束的回调函数  
        })
      }
    })
  },
});
